package com.li.controller.admin;

import com.li.DTO.BuildingManageDTO;
import com.li.VO.PageListVO;
import com.li.pojo.BuildingManage;
import com.li.result.Result;
import com.li.service.BuildingManageService;
import com.li.util.ExcelPoi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author: zhiqingLi
 * @Description: 楼宇管理
 * @Date: Created in 14:34 2024/3/13
 **/
@RestController
@Slf4j
@RequestMapping("/admin/build")
public class BuildingManageController {

    @Autowired
    private BuildingManageService buildingManageService;

    //查询全部 加上模糊查询
    @PostMapping("/selShow")
    public Result<PageListVO> showList(@RequestBody BuildingManageDTO buildingManageDTO){
        log.info("信息传递：{}",buildingManageDTO);
        PageListVO pageListVO =  buildingManageService.showList(buildingManageDTO);
        return Result.success(pageListVO);
    }

    //添加
    @PostMapping("/add")
    public Result addSubregion(@RequestBody BuildingManage buildingManage){
        log.info("信息传递：{}",buildingManage);
        buildingManageService.addSubregion(buildingManage);
        return Result.success();
    }

    //修改回显
    @GetMapping("/upd/{id}")
    public Result<BuildingManage> updGetById(@PathVariable("id") Long id){
        log.info("信息传递：{}",id);
        BuildingManage buildingManage = buildingManageService.updGetById(id);
        return Result.success(buildingManage);
    }

    //修改数据
    @PostMapping("/updSubregion")
    public Result updSubregion(@RequestBody BuildingManage buildingManage){
        log.info("信息传递：{}",buildingManage);
        buildingManageService.updSubregion(buildingManage);
        return Result.success();
    }

    //删除
    @GetMapping("/del/{id}")
    public Result delById(@PathVariable("id") Long id){
        log.info("信息传递：{}",id);
        buildingManageService.delById(id);
        return Result.success();
    }

    //回显数据小区名字
    @GetMapping("/showName")
    public Result<List<String>> showName(){
        List<String> stringList = buildingManageService.showName();
        return Result.success(stringList);
    }

    //回显楼宇
    @GetMapping("/showInter")
    public Result<List<Integer>> showInter(){
        List<Integer> stringList = buildingManageService.showInter();
        return Result.success(stringList);
    }

    //上传 excel 表格数据 // id 必然为2
    @PostMapping("/fileUpload")
    public Result<String> fileUpload(@RequestPart("file")MultipartFile file,@RequestParam("id")Integer id) throws Exception {
        InputStream inputStream = file.getInputStream();
        List<BuildingManage> read = ExcelPoi.read(inputStream, id);
        if (read.size()>0){
            for (BuildingManage buildingManage : read) {
                buildingManageService.addSubregion(buildingManage);
            }
        }
        return Result.success();
    }
}
